import { ref } from "vue";

// 菜单数据
export const menuItems = ref([
  {
    index: "1",
    path: "/home",
    title: "Dashboard",
    icon: "House",
  },
  {
    index: "2",
    title: "系统管理",
    icon: "Setting",
    children: [
      {
        index: "2-1",
        path: "/task",
        title: " - 需求任务管理",
      },
      {
        index: "2-2",
        path: "/test2",
        title: " - 测试用例管理",
      },
    ],
  },
]);

// 根据当前路由路径找到匹配的菜单项
export function findMenuPath(path) {
  const result = [];

  // 如果是首页，返回空数组
  if (path === "/" || path === "/home") {
    return [];
  }

  // 遍历菜单项，查找匹配的路由
  for (const menu of menuItems.value) {
    // 检查一级菜单
    if (menu.path === path) {
      // 如果是非首页的一级菜单，添加到结果中
      if (menu.path !== "/home") {
        result.push({ title: menu.title, path: menu.path });
      }
      break;
    }

    // 检查二级菜单
    if (menu.children && menu.children.length > 0) {
      const matchedSubMenu = menu.children.find(
        (subMenu) => subMenu.path === path
      );
      if (matchedSubMenu) {
        // 添加父菜单
        result.push({ title: menu.title, path: menu.path || "#" });
        // 添加子菜单
        result.push({ title: matchedSubMenu.title, path: matchedSubMenu.path });
        break;
      }
    }
  }

  return result;
}
